//
//  CMDBHomePopViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/14.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let publishBtnHW : CGFloat = 57

private let colCunt : Int          = 3

class CMDBHomePopViewController: CMDBBaseViewController {
    
    let publicTitleArr : [String] = ["动态","日记","相片","档期","简历"]
    
    var selectedImages : [UIImage]?
    
    var superVC : UIViewController?

    override func setup() {
        super.setup()
        
        setupPusblisthButtons()
        
        view.backgroundColor = UIColor.Hex6(0xffffff, alpha: 0.87)
    }

}

extension CMDBHomePopViewController {
    
    func setupPusblisthButtons(){
        
        for (index,title) in publicTitleArr.enumerate() {
         
            let publishButn = CMDBPublishButton()
            
            let row         = index / colCunt
            
            let col         = index % colCunt
            
            let colWith     = CMDB_Screen_Width - 2 * KleftMargin
            
            let btnMargin   = (colWith - CGFloat(colCunt) * publishBtnHW - 40 * 2) * 0.5
            
            publishButn.setTitle(title, forState: .Normal)
            
            publishButn.setImage(UIImage(named:title), forState: .Normal)
            
            publishButn.addTarget(self, action: #selector(publish(_:)), forControlEvents: .TouchUpInside)
            
            publishButn.frame = CGRect(x: 40 + (publishBtnHW + btnMargin) * CGFloat(col), y: 40 + CGFloat(row) * (publishBtnHW + 39) , width: publishBtnHW, height: publishBtnHW + 17)

            view.addSubview(publishButn)
            
        }
    }
}


extension CMDBHomePopViewController{
    
    @objc func popDismiss(){
        
        dismissViewControllerAnimated(true) { 
            
        }
        
    }
    
    @objc func publish(clickedBtn : UIButton){
        
        let btnTitle              = clickedBtn.titleForState(.Normal)
        
        var publishedController   = CMDBBasePublishViewController()
        
        if btnTitle == "动态" {
            
            publishedController       = CMDBPublishStatusViewController()
            
            publishedController.title = btnTitle
            
            self.dismissViewControllerAnimated(true, completion: {
                            })
            
            self.superVC!.presentViewController(publishedController, animated: true,completion: nil)
            
        }else if btnTitle == "日记"{
            
            publishedController       = CMDBPublishJournalController()
            
            publishedController.title = btnTitle
            
            self.dismissViewControllerAnimated(true, completion: {
            })
            
            self.superVC!.presentViewController(publishedController, animated: true,completion: nil)
            
        }else if btnTitle == "相片"{
            
//            addImage()
            
            self.dismissViewControllerAnimated(true, completion: {
            })
            
            let superTabbarVc = superVC as! CMDBTabBarController
            
            superTabbarVc.addImage()
            
        }
    }
    
}

extension CMDBHomePopViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
//    func addImage(){
//        
//        if !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
//            return
//        }
//        
////        self.dismissViewControllerAnimated(true, completion: {
////        })
//        
//        self.presentViewController(self.imagePickerController, animated: true,completion: nil)
//
//
//    }
}

extension CMDBHomePopViewController : ZYQAssetPickerControllerDelegate{
    
    func assetPickerController(picker: ZYQAssetPickerController!, didFinishPickingAssets assets: [AnyObject]!) {
//
//        selectedImages = [UIImage]()
//        
//        for (_,asset) in assets.enumerate() {
//            
//            let assetA     = asset as! ALAsset
//            
//            let unMCgImage = assetA.defaultRepresentation().fullScreenImage()
//
//            let cgImage    = unMCgImage.takeUnretainedValue()
//            
//            let image      = UIImage(CGImage: cgImage)
//        
//            selectedImages?.append(image)
//        }
//        
//        let addImageVc     = CMDBPublishPhotosController()
//        
//        addImageVc.images  = selectedImages!
//        
////        picker.pushViewController(addImageVc, animated: true)
//
//        presentedViewController?.dismissViewControllerAnimated(true, completion: {
//            self.superVC!.presentViewController(addImageVc, animated: true,completion: nil)
//        })
    }
//
//    func assetPickerControllerDidMaximum(picker: ZYQAssetPickerController!) {
//        let alertView = UIAlertView.init(title: "提示", message: "最多选择九张图片", delegate: nil, cancelButtonTitle: "知道了")
//        
//        alertView.show()
//        
//    }
    
}

